<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" label-width="220px" :model="employeeForm" :rules="rules">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input size="mini" class="inputW" v-model="employeeForm.username" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <!-- 工号是系统生成的，不能被修改 -->
              <el-form-item label="工号" prop="workNumber">
                <el-input disabled size="mini" class="inputW" v-model="employeeForm.workNumber" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input size="mini" class="inputW" v-model="employeeForm.mobile" 
                :disabled="!!$route.params.id"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                 <!-- inputW样式会给到selectTree中，template第一层的组件 -->
                <SelectTree class="inputW" v-model="employeeForm.departmentId" :value="employeeForm.departmentId"></SelectTree>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select size="mini" class="inputW" v-model="employeeForm.formOfEmployment">
                  <el-option label="正式" :value="1"></el-option>
                  <el-option label="非正式" :value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker size="mini" type="date" value-format="yyyy-MM-dd" class="inputW"
                  v-model="employeeForm.timeOfEntry" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker size="mini" type="date" class="inputW" v-model="employeeForm.correctionTime" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                 <image-upload v-model="employeeForm.staffPhoto" ></image-upload>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="saveData">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import SelectTree from './components/select-tree.vue'
import { addEmployee,getEmployeeDetail,updateEmployee } from '@/api/employee'
import imageUpload from './components/image-upload.vue'

export default {
  components:{
    SelectTree,
    imageUpload
  },
  
  data() {
    return {
      employeeForm: {
        username: '',
        mobile: '',
        formOfEmployment: null,
        workNumber: '',
        departmentId:null,
        timeOfEntry: '',
        correctionTime: '',
        staffPhoto:''
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '长度在1-4个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'blur' }
        ],
        departmentId: [{ required: true, message: '请选择部门', trigger: 'blur' }],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' },
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'blur' },

          {
            trigger: 'blur',
            validator: (rule, value, callback) => {
              if (this.employeeForm.timeOfEntry) {
                if (new Date(value) < new Date(this.employeeForm.timeOfEntry)) {
                  callback(new Error('转正时间不能早于入职时间'))
                }
              }
              callback()
            }
          }

        ],

      }
    }
  },
  methods: {
    saveData() {
      this.$refs.userForm.validate(async valid => {
        if (valid){
          if (this.$route.params.id){
            await updateEmployee(this.employeeForm)
            this.$message.success('更新成功')
          }else{
            await addEmployee(this.employeeForm)
            this.$message.success('新增成功')
          }

          this.$router.push('/employee')
        }
      })
    },
    async getEmployeeDetail() {
     this.employeeForm =  await getEmployeeDetail(this.$route.params.id)
    }

  },
  created(){
  this.$route.params.id&&this.getEmployeeDetail()
    
 
    
    
  }
}
</script>

<style scoped lang="scss">
.edit-form {
  background: #fff;
  padding: 20px;

  .inputW {
    width: 380px
  }
}
</style>